<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class CI_Volume_discount_entity{
	var $current_dbprefix = "";
	var $id = 0;
	var $agent_id = 0;
	var $agent_username = '';
	var $agent_name = '';
	var $rate_from = 0;
	var $rate_to = 0;
	var $actual_registered = 0;
	var $active_from = 0;
	var $active_to = 0;
	var $discount_amount_per_register = 0;
	var $discount_unit = 0;
	var $created_time = 0;
	var $active = 0;
	
	function __construct() {
		$ci= & get_instance();
		$this->current_dbprefix = $ci->db->dbprefix;
		$ci->db->set_dbprefix('default_');
	}
	function __destruct() {
		$ci= & get_instance();
		$ci->db->set_dbprefix($this->current_dbprefix);
	}
	
	function setId($input){
		$this->id=$input;
	}
	function setAgentId($input){
		$this->agent_id = $input;
	}
	function setAgentUsername($input){
		$this->agent_username = $input;
	}
	function setAgentName($input){
		$this->agent_name = $input;
	}
	function setRateFrom($input){
		$this->rate_from = $input;
	}
	function setRateTo($input){
		$this->rate_to = $input;
	}
	function setActualRegistered($input){
		$this->actual_registered = $input;
	}
	function setActiveFrom($input){
		$this->active_from = $input;
	}
	function setActiveTo($input){
		$this->active_to = $input;
	}
	function setDiscountAmountPerRegister($input){
		$this->discount_amount_per_register = $input;
	}
	function setDiscountUnit($input){
		$this->discount_unit = $input;
	}
	function setCreatedTime($input){
		$this->created_time = $input;
	}
	function setActive($input){
		$this->active = $input;
	}
	
	function getId(){
		return $this->id;
	}
	function getAgentId(){
		return $this->agent_id;
	}
	function getAgentUsername(){
		return $this->agent_username;
	}
	function getAgentName(){
		return $this->agent_name;
	}
	function getRateFrom(){
		return $this->rate_from;
	}
	function getRateTo(){
		return $this->rate_to;
	}
	function getActualRegistered(){
		return $this->actual_registered;
	}
	function getActiveFrom(){
		return $this->active_from;
	}
	function getActiveTo(){
		return $this->active_to;
	}
	function getDiscountAmountPerRegister(){
		return $this->discount_amount_per_register;
	}
	function getDiscountUnit(){
		return $this->discount_unit;
	}
	function getCreatedTime(){
		return $this->created_time;
	}
	function getActive(){
		return $this->active;
	}
	
	function init($cmd, $input)
	{
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		if($cmd == 'id')
		{
			$result = $ci->db->where('id',$input)->get(TBL_VOLUME_DISCOUNT)->result();
		}else{
			return false;
		}
		if( !empty($result) )
		{
			$this->id = $result[0]->id;
			$this->agent_id = $result[0]->agent_id;
			$this->agent_username = $result[0]->agent_username;
			$this->agent_name = $result[0]->agent_name;
			$this->rate_from = $result[0]->rate_from;
			$this->rate_to = $result[0]->rate_to;
			$this->actual_registered = $result[0]->actual_registered;
			$this->active_from = $result[0]->active_from;
			$this->active_to = $result[0]->active_to;
			$this->discount_amount_per_register = $result[0]->discount_amount_per_register;
			$this->discount_unit = $result[0]->discount_unit;
			$this->created_time = $result[0]->created_time;
			$this->active = $result[0]->active;
			return $this;
		}
		return false;
	}
	
	function getList($limit = array()){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		if(count($limit) >0){
			$result = $ci->db->limit($limit[0], $limit[1]);
		}
		$ci->db->order_by('created_time', 'DESC');
		return $ci->db->get(TBL_VOLUME_DISCOUNT)->result();
	}
	function count(){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		return $ci->db->count_all_results(TBL_VOLUME_DISCOUNT);
	}
	
	function save(){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		$ci->db->set('agent_id',$this->getCommercialId());
		$ci->db->set('agent_username',$this->getAgentUsername());
		$ci->db->set('agent_name',$this->getAgentName());
		$ci->db->set('rate_from',$this->getRateFrom());
		$ci->db->set('rate_to',$this->getRateTo());
		$ci->db->set('actual_registered',$this->getActualRegistered());
		$ci->db->set('active_from',$this->getActiveFrom());
		$ci->db->set('active_to',$this->getActiveTo());
		$ci->db->set('discount_amount_per_register',$this->getDiscountAmountPerRegister());
		$ci->db->set('discount_unit',$this->getDiscountUnit());
		$ci->db->set('created_time',$this->getCreatedTime());
		$ci->db->set('active',$this->getActive());
		$ci->db->insert(TBL_VOLUME_DISCOUNT);
		return $ci->db->insert_id();
	}
	function update(){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		$ci->db->set('agent_id',$this->getCommercialId());
		$ci->db->set('agent_username',$this->getAgentUsername());
		$ci->db->set('agent_name',$this->getAgentName());
		$ci->db->set('rate_from',$this->getRateFrom());
		$ci->db->set('rate_to',$this->getRateTo());
		$ci->db->set('actual_registered',$this->getActualRegistered());
		$ci->db->set('active_from',$this->getActiveFrom());
		$ci->db->set('active_to',$this->getActiveTo());
		$ci->db->set('discount_amount_per_register',$this->getDiscountAmountPerRegister());
		$ci->db->set('discount_unit',$this->getDiscountUnit());
		$ci->db->set('created_time',$this->getCreatedTime());
		$ci->db->set('active',$this->getActive());
		return $ci->db->where('id', $this->id)->update(TBL_VOLUME_DISCOUNT);
	}
	function delete($id){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		$ci->db->where('id',$id);
		$ci->db->delete(TBL_VOLUME_DISCOUNT);
	}
	
	function getDiscountOfAgent($agent_id, $number_escorts){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		$time = time();
		$rs = $ci->db->where("agent_id = $agent_id AND active_from <= $time AND active_to >= $time AND active=1")
				->get(TBL_VOLUME_DISCOUNT)->result();
		$id= 0;
		if(!$rs) return false;
		
		foreach($rs as $item){
			if($item->rate_from <= $number_escorts AND $item->rate_to >= $number_escorts){
				$id = $item->id;
				break;
			}
		}
		return ($id!=0) ? $this->init('id',$id):false;
	}
	public function countActiveVolumeDiscount(){
		$ci=& get_instance();
		$ci->db->set_dbprefix('default_');
		
		$ci->db->where('active', 1);
		return $ci->db->count_all_results(TBL_VOLUME_DISCOUNT);
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	//endclass
}